import { router } from '@/router/index'
import nprpgreee from 'nprogress'
import 'nprogress/nprogress.css'
import { useMenuStoreWithOut } from '@/store/modules/menu'

const menuStore = useMenuStoreWithOut()

export function setupRouterHooks () {
  router.beforeEach((to, from, next) => {
    nprpgreee.start() // 开始加载进度条
    if (to.path === '/login') {
      next()
    } else {
      // 页面刷新时，重新加载路由
      if (menuStore.menuList.length === 0) {
        menuStore.setMenuList()
        // console.log(router.getRoutes())
        next({ path: to.path })
        // next({ ...to, replace: true }) //此方法不生效
      } else {
        next()
      }
      // if (to.meta.requiresAuth && !userInfoStore.user) {
      //   // 此路由需要授权，请检查是否登录，如果没有，则重定向到登录页
      //   return { path: '/login', query: { redirect: to.fullPath } } // 保存当前位置，
      // } else {
      // }
    }
  })
  router.afterEach(() => {
    nprpgreee.done()
  })
}
